<?php

namespace app\index\library\traits;

trait ArticleDigitalslides
{

    public function addArticle()
    {
        if ($this->request->isPost()) {
            $data['user_id'] = $this->userID;
            $data['title'] = $this->request->post('title');
            $data['content'] = $this->request->post('content');
            $model = new \app\admin\model\user\Article;
            $model->save($data);
            $this->success('添加成功', '/index/user/index/ids/'.$this->userID.'/act/index.html');
        }
        return $this->view->fetch('user/article/add');
    }

    public function editArticle($ids = null)
    {
        $model = new \app\admin\model\user\Article;
        $row = $model->get($ids);
        if ($this->request->isPost()) {
            $data['title'] = $this->request->post('title');
            $data['content'] = $this->request->post('content');
            if ($row->save($data)) {
                $row->setInc('editnum');
                $this->success('更新成功', '/index/user/index/ids/'.$this->userID.'/act/index.html');
            }
        }
        if ($row['files'])
            $row['files'] = json_decode($row['files'], true);
        $this->view->assign('row', $row);
        return $this->view->fetch('user/article/edit');
    }

    public function detailArticle($ids = null)
    {
        $model = new \app\admin\model\user\Article;
        $row = $model->with('users')->where('id', $ids)->find();
        $row->setInc('hits');
        if ($row['files'])
            $row['files'] = json_decode($row['files'], true);

        $this->view->assign('row', $row);
        return $this->view->fetch('user/article/detail');
    }

    public function addDigitalslides()
    {
        if ($this->request->post()) {
            $data['user_id'] = $this->userID;
            $data['title'] = $this->request->post('title');
            $data['content'] = $this->request->post('content');
            $data['files'] = check_digitalslides($this->request->post('file/a'));
            $model = new \app\admin\model\user\Digitalslides;
            $model->save($data);
            $this->success('发布成功');
        }
        return $this->view->fetch('user/digitalslides/add');
    }

    public function editDigitalslides($ids = null)
    {
        $model = new \app\admin\model\user\Digitalslides;
        $row = $model->get($ids);
        if ($this->request->isPost()) {
            $data['title'] = $this->request->post('title');
            $data['content'] = $this->request->post('content');
            $data['files'] = check_digitalslides($this->request->post('file/a'));
            if ($row->save($data)) {
                $row->setInc('editnum');
                $this->success('更新成功', '/index/user/index/ids/'.$this->userID.'/act/digitalslides.html');
            }
        }
        if ($row['files'])
            $row['files'] = json_decode($row['files'], true);
        $this->view->assign('row', $row);
        return $this->view->fetch('user/digitalslides/edit');
    }

    public function detailDigitalslides($ids = null)
    {
        $model = new \app\admin\model\user\Digitalslides;
        $row = $model->with('users')->where('id', $ids)->find();
        $row->setInc('hits');
        $row['files'] = json_decode($row['files'], true);
        $this->view->assign('row', $row);
        return $this->view->fetch('user/digitalslides/detail');
    }
}
